<?php

namespace App\Admin\Controllers;

use App\Admin\Models\City;
use App\Admin\Models\Shop;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ShopController extends Controller
{
    use HasResourceActions;
    
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('店铺')
            ->description(trans('admin.list'))
            ->body($this->grid());
    }
    
    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('店铺')
            ->description(trans('admin.view'))
            ->body($this->detail($id));
    }
    
    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('店铺')
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }
    
    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('店铺')
            ->description(trans('admin.create'))
            ->body($this->form());
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Shop);
        
        $grid->id('ID')->sortable();
        $grid->name('店铺名称');
        $grid->column('city.name', '城市');
        
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('name', '名称');
            $filter->in('city', '城市')->select(
                City::getMaps('id', 'name')
            );
        });
        
        return $grid;
    }
    
    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Shop::findOrFail($id));
        
        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        
        return $show;
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Shop);
        
        $form->display('id', 'ID');
        $form->text('name', '名称')->rules('required');
        $form->text('phone', '电话');
        $form->hidden('on_time');
        $form->timeRange('on_time_start', 'on_time_end', '营业时间');
        $form->text('address', '地址');
        $form->select('city_id', '城市')->options(City::getMaps('id', 'name'))->rules('required');
        $form->image('picture', '图片');
        $form->display('created_at', '创建时间');
        $form->display('updated_at', '修改时间');
        
        $form->submitted(function (Form $form) {
            $form->on_time = request('on_time_start') . '-' . request('on_time_end');
            $form->ignore('on_time_start');
            $form->ignore('on_time_end');
        });
        
        return $form;
    }
}
